<template>
  <div class="look-over">
    <van-nav-bar
      left-arrow
      class="title-bar"
      title="文件详情"
      :border="false"
      @click-left="$router.push('/layout')"
    />
    <!-- 展开 -->
    <div class="title-unfold">
      <div>
        <span>{{ title }}</span>
        <van-icon
          v-if="rate === 100"
          class-prefix="iconfont iconxiazaitubiao"
          @click="convertBase64UrlToFile"
        />
      </div>
      <div>
        <span
          >状态:
          <span :class="rate === 100 ? 'colour-type' : ''">{{
            rate === 100 ? '签约成功' : '签约中'
          }}</span></span
        >
        <span v-show="!AFold" @click="AFold = !AFold"
          >展开<van-icon name="arrow-down"
        /></span>
      </div>
    </div>
    <!-- 收起 -->
    <div class="pack-up" v-if="AFold">
      <div class="box-circle">
        <van-circle v-model="currentRate" :rate="rate" :speed="100">
          <div class="default" slot="default">
            <span>{{ text }}</span> 签约完成度
          </div>
        </van-circle>
        <div class="box-right">
          <span>发件方: {{ contractBaseInfo.sponsorName }}</span>
          <span>发起人: {{ contractBaseInfo.sponsorMobile }}</span>
          <span>发起时间: {{ contractBaseInfo.createTime }}</span>
          <span>截止时间: {{ contractBaseInfo.signingEndTime }}</span>
        </div>
      </div>
      <span class="pack-up-span" @click="AFold = !AFold"
        >收起<van-icon name="arrow-up"
      /></span>
    </div>
    <div class="participant"><span>参与方</span></div>
    <!-- 参与方 -->
    <div class="Administration">
      <!-- 企业 -->
      <div
        class="Administration-box"
        v-for="(item, index) in signatoryInfoList"
        :key="index"
      >
        <div class="Administration-title">{{ item.orgName }}</div>
        <div class="Administration-content">
          <div>
            <span>{{ item.userName }}</span>
            <span>{{ item.userMobile }}</span>
            <span :class="item.signerState === 1 ? '' : 'color-span'">{{
              item.signerState === 1 ? '已签署' : '未签署'
            }}</span>
          </div>
          <div>
            <span>{{ item.signTime }}</span>
          </div>
        </div>
      </div>
    </div>
    <div class="participant"><span>抄送方</span></div>
    <!-- 抄送方 -->
    <div
      class="plagiarize"
      v-for="(item, index) in recipientInfoList"
      :key="index"
    >
      <span>{{ item.orgName }} ({{ item.operatorContact }})</span>
    </div>
    <div class="participant"><span>签约文件</span></div>
    <!-- 签约文件 -->
    <div class="thumbnail">
      <img
        :src="'data:image/png;base64,' + contractImg[0]"
        alt=""
        @click="Preview"
      />
      <span>{{ title }}</span>
      <span>共{{ contractImg.length }}页</span>
    </div>
    <!-- 底部 -->
    <div class="end">
      <span class="line"></span>
      <span>没有更多了</span>
      <span class="line"></span>
    </div>
  </div>
</template>

<script>
import {
  addContractMessage,
  addContractPreview,
  addContractDownload,
  addContractCompleteness
} from '@/api/contract'
import { ImagePreview } from 'vant'
export default {
  data() {
    return {
      activeNames: [],
      // 抄送方
      recipientInfoList: [],
      // 参与方
      signatoryInfoList: [],
      // 待签名文件列表
      fileList: {},
      // 合同基本信息
      contractBaseInfo: {},
      // 展示第一个文件
      curFile: {
        contractId: '',
        fileId: '',
        filePath: '',
        filePages: []
      },
      // 合同图片
      contractImg: [],
      // 合同标题
      title: '',
      AFold: false,
      currentRate: 0,
      rate: 0
    }
  },
  created() {
    this.ContractList()
    this.completeness()
  },
  mounted() {},
  methods: {
    async ContractList() {
      // 合同信息
      try {
        const { data: res } = await addContractMessage({
          contractId: this.$route.params.contractId
        })
        if (res.code !== '0') {
          return this.$toast.fail('合同信息失败')
        }
        this.contractAllInfo = res.data

        // 抄送方收件人
        this.recipientInfoList = this.contractAllInfo.recipientInfoList
        // 合同签署人信息
        this.signatoryInfoList = this.contractAllInfo.signatoryInfoList
        // 合同基本信息
        this.contractBaseInfo = this.contractAllInfo.contractBaseInfo

        // 待签名文件列表
        const tempFileArr = []
        this.contractAllInfo.signingFileInfoList.forEach(item => {
          const file = {
            fileId: item.fileId,
            fileName: item.fileName,
            filePath: item.filePath,
            filePages: item.filePages
          }
          tempFileArr.push(file)
        })
        this.fileList = tempFileArr
        this.title = this.fileList[0].fileName

        // 展示第一个文件
        this.curFile.contractId = this.contractAllInfo.contractBaseInfo.contractId
        this.curFile.fileId = this.fileList[0].fileId
        this.curFile.filePath = this.fileList[0].filePath
        const pages = []
        for (var i = 0; i <= this.fileList[0].filePages; i++) {
          pages.push(i + 1)
        }
        this.curFile.filePages = pages

        // 获取合同图片
        for (let i = 1; i < this.curFile.filePages.length; i++) {
          await addContractPreview({
            // contractId: this.curFile.contractId,
            filePath: this.curFile.filePath,
            pageNum: i
          }).then(res => {
            if (res.data.code !== '0') {
              return this.$toast.fail('图片获取失败!')
            }
            this.contractImg.push(res.data.data)
          })
        }
      } catch (error) {
        console.log(error)
      }
    },
    // 下载合同
    async convertBase64UrlToFile() {
      try {
        const { data: res } = await addContractDownload({
          contractId: this.$route.params.contractId
        })
        if (res.code !== '0') {
          return this.$toast.fail('下载失败')
        }
        let url = `data:image/png;base64,${res.data}`
        const downloadFileName = 'contract.pdf'
        if (window.navigator.msSaveBlob) {
          try {
            window.navigator.msSaveBlob(url, downloadFileName)
          } catch (e) {
            console.log(e)
          }
        } else {
          // 谷歌浏览器 创建a标签 添加download属性下载
          if (typeof url === 'object' && url instanceof Blob) {
            url = URL.createObjectURL(url) // 创建blob地址
          }
          var aLink = document.createElement('a')
          aLink.href = url
          aLink.download = 'contract.pdf' || '' // HTML5新增的属性，指定保存文件名，可以不要后缀，注意，file:///模式下不会生效
          var event
          if (window.MouseEvent) {
            event = new MouseEvent('click')
          } else {
            event = document.createEvent('MouseEvents')
            event.initMouseEvent(
              'click',
              true,
              false,
              window,
              0,
              0,
              0,
              0,
              0,
              false,
              false,
              false,
              false,
              0,
              null
            )
          }
          aLink.dispatchEvent(event)
        }
      } catch (error) {
        console.log(error)
      }
    },
    // 合同预览
    Preview() {
      const list = this.contractImg.map(item => 'data:image/png;base64,' + item)
      ImagePreview({
        images: [...list],
        closeable: true
      })
    },
    // 合同完成度
    async completeness() {
      try {
        const { data: res } = await addContractCompleteness({
          contractId: this.$route.params.contractId
        })
        if (res.code !== '0') {
          return this.$toast.fail('数据获取失败')
        }
        this.rate = res.data.progressPercentage * 100
      } catch (error) {
        console.log(error)
      }
    }
  },
  computed: {
    text() {
      return `${this.currentRate.toFixed(0)}%`
    }
  }
}
</script>

<style scoped lang="less">
.look-over {
  .title-bar {
    height: 128px;
    width: 100%;
    padding-top: 40px;
    box-sizing: border-box;
    margin-bottom: 20px;
    /deep/.van-nav-bar__title {
      font-size: 34px;
      font-weight: bold;
    }
    /deep/.van-icon {
      font-size: 48px;
      color: #000;
    }
  }
  // 展开
  .title-unfold {
    height: 170px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: 0 40px;
    div:nth-child(1) {
      font-size: 36px;
      margin-bottom: 16px;
      display: flex;
      justify-content: space-between;
      .iconxiazaitubiao {
        font-size: 30px;
        color: #2489f2;
      }
    }
    div:nth-child(2) {
      font-size: 26px;
      display: flex;
      justify-content: space-between;
      color: #999999;
      .colour-type {
        color: #2489f2;
      }
    }
  }
  // 收起
  .pack-up {
    height: 340px;
    padding: 0 40px;
    position: relative;
    .box-circle {
      height: 240px;
      width: 100%;
      background-color: #f6f6f6;
      display: flex;
      .van-circle {
        width: 158px;
        height: 158px;
        margin: 55px 35px 0 35px;
      }
      .default {
        height: 100%;
        display: flex;
        flex-direction: column;
        justify-content: center;
        font-size: 18px;
        span {
          font-size: 30px;
          font-weight: bold;
        }
      }
      .box-right {
        font-size: 24px;
        display: flex;
        flex-direction: column;
        justify-content: center;
      }
    }
    .pack-up-span {
      position: absolute;
      bottom: 44px;
      right: 40px;
      font-size: 26px;
      color: #999999;
    }
  }

  // 参与方
  .participant {
    padding: 20px 41px;
    margin-top: -1px;
    font-size: 26px;
    color: #9f9f9f;
    background-color: #f6f6f6;
  }
  // 参与方
  .Administration {
    padding-left: 40px;
    .Administration-box {
      height: 165px;
      box-sizing: border-box;
      padding-right: 28px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      border-bottom: 1px solid #e4e4e4;
      .Administration-title {
        font-size: 30px;
        margin-bottom: 12px;
      }
      .Administration-content {
        font-size: 26px;
        color: #999999;
        display: flex;
        justify-content: space-between;
        span {
          margin-right: 12px;
        }
        .color-span {
          color: #2489f2;
        }
      }
    }
  }
  // 抄送方
  .plagiarize {
    height: 100px;
    line-height: 100px;
    font-size: 26px;
    padding-left: 41px;
    color: #9f9f9f;
  }
  // 签约文件
  .thumbnail {
    height: 348px;
    padding: 28px 41px;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    img {
      width: 300px;
      height: 200px;
      margin-bottom: 16px;
      border: 1px solid #e0e0e0;
    }
    span:nth-child(2) {
      font-size: 26px;
    }
    span:nth-child(3) {
      font-size: 24px;
      color: #999999;
    }
  }
  .end {
    height: 92px;
    background-color: #f6f6f6;
    font-size: 22px;
    text-align: center;
    line-height: 92px;
    color: #999;
    .line {
      display: inline-block;
      width: 48px;
      height: 1px;
      border-bottom: 1px solid #999;
      vertical-align: middle;
      margin: 0 12px;
    }
  }
}
</style>
